#
# Copyright (c) 2011-2013, fortiss GmbH.
# Licensed under the Apache License, Version 2.0.
#
# Use, modification and distribution are subject to the terms specified
# in the accompanying license file LICENSE.txt located at the root directory
# of this software distribution. A copy is available at
# http://chromosome.fortiss.org/.
#
# This file is part of CHROMOSOME.
#
# $Id: CMakeLists.txt 7657 2014-03-03 16:58:19Z wiesmueller $
#
# File:
#         CMake configuration script for Microsoft(R) Visual C/C++ compiler
#         toolchain.
#

if (NOT MSVC)
	xme_message (FATAL_ERROR "Expected Microsoft(R) Visual C/C++ compiler toolchain, but MSVC is not set! You probably need to select a different XME_TARGET_IDENTIFIER (currently set to '${XME_TARGET_IDENTIFIER}') or a different CMake generator!")
endif ()

set (MSVC_CODE_ANALYSIS_SUPPORT FALSE CACHE BOOL "Whether code analysis support should be enabled in debug builds, requires Visual Studio Ultimate/Team System.")

xme_include_directory()

# warning C4127 (level 4): conditional expression is constant (e.g., do { ... } while (0); )
# warning C4702: unreachable code (e.g., expansion of XME_ASSERT())
string(STRIP "${CMAKE_C_FLAGS}" CMAKE_C_FLAGS_STRIPPED)
string(STRIP "${CMAKE_C_FLAGS_INIT}" CMAKE_C_FLAGS_INIT_STRIPPED)
if((NOT CMAKE_C_FLAGS) OR (CMAKE_C_FLAGS_STRIPPED STREQUAL "") OR (CMAKE_C_FLAGS_STRIPPED STREQUAL CMAKE_C_FLAGS_INIT_STRIPPED))
    set (CMAKE_C_FLAGS "/WX /W3 /wd4127 /wd4702" CACHE STRING "" FORCE)
endif()

string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS_STRIPPED)
string(STRIP "${CMAKE_CXX_FLAGS_INIT}" CMAKE_CXX_FLAGS_INIT_STRIPPED)
if((NOT CMAKE_CXX_FLAGS) OR (CMAKE_CXX_FLAGS_STRIPPED STREQUAL "") OR (CMAKE_CXX_FLAGS_STRIPPED STREQUAL CMAKE_CXX_FLAGS_INIT_STRIPPED))
    set (CMAKE_CXX_FLAGS "/WX /W3 /wd4127 /wd4702 /EHs" CACHE STRING "" FORCE)
endif()

xme_set (PROPERTY_GLOBAL XME_LINKER_FLAGS_MAP_FILE "/MAP")

if (${XME_TARGET_CPU} STREQUAL "x86")
    if (NOT CMAKE_EXE_LINKER_FLAGS OR (CMAKE_EXE_LINKER_FLAGS STREQUAL " ") OR (CMAKE_EXE_LINKER_FLAGS STREQUAL CMAKE_EXE_LINKER_FLAGS_INIT))
        set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MACHINE:X86" CACHE STRING "" FORCE)
    endif ()
elseif (${XME_TARGET_CPU} STREQUAL "x86_64")
    if (NOT CMAKE_EXE_LINKER_FLAGS OR (CMAKE_EXE_LINKER_FLAGS STREQUAL " ") OR (CMAKE_EXE_LINKER_FLAGS STREQUAL CMAKE_EXE_LINKER_FLAGS_INIT))
        set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MACHINE:X64" CACHE STRING "" FORCE)
    endif ()

    set (CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   /DIA64 /env x64" CACHE STRING "" FORCE)
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DIA64 /env x64" CACHE STRING "" FORCE)
endif (${XME_TARGET_CPU} STREQUAL "x86")

if(NOT CMAKE_C_FLAGS_DEBUG OR (CMAKE_C_FLAGS_DEBUG STREQUAL " ") OR (CMAKE_C_FLAGS_DEBUG STREQUAL CMAKE_C_FLAGS_DEBUG_INIT))
    set (CMAKE_C_FLAGS_DEBUG   "/DDEBUG /Od /Ob0 /RTC1 /ZI /GS /GA" CACHE STRING "" FORCE)
    if (MSVC_CODE_ANALYSIS_SUPPORT)
        set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /analyze /analyze:WX-" CACHE STRING "" FORCE)
    endif ()
endif()

if(NOT CMAKE_CXX_FLAGS_DEBUG OR (CMAKE_CXX_FLAGS_DEBUG STREQUAL " ") OR (CMAKE_CXX_FLAGS_DEBUG STREQUAL CMAKE_CXX_FLAGS_DEBUG_INIT))
    set (CMAKE_CXX_FLAGS_DEBUG "/DDEBUG /Od /Ob0 /RTC1 /ZI /GS /GA" CACHE STRING "" FORCE)
    if (MSVC_CODE_ANALYSIS_SUPPORT)
        set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /analyze /analyze:WX-" CACHE STRING "" FORCE)
    endif ()
endif()

if(NOT CMAKE_C_FLAGS_RELEASE OR (CMAKE_C_FLAGS_RELEASE STREQUAL " ") OR (CMAKE_C_FLAGS_RELEASE STREQUAL CMAKE_C_FLAGS_RELEASE_INIT))
    set (CMAKE_C_FLAGS_RELEASE "/DNDEBUG /GS /GA" CACHE STRING "" FORCE)
endif()

if(NOT CMAKE_CXX_FLAGS_RELEASE OR (CMAKE_CXX_FLAGS_RELEASE STREQUAL " ") OR (CMAKE_CXX_FLAGS_RELEASE STREQUAL CMAKE_CXX_FLAGS_RELEASE_INIT))
    set (CMAKE_CXX_FLAGS_RELEASE "/DNDEBUG /GS /GA" CACHE STRING "" FORCE)
endif()

if(NOT CMAKE_C_FLAGS_MINSIZEREL OR (CMAKE_C_FLAGS_MINSIZEREL STREQUAL " ") OR (CMAKE_C_FLAGS_MINSIZEREL STREQUAL CMAKE_C_FLAGS_MINSIZEREL_INIT))
    set (CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG" CACHE STRING "" FORCE)
endif()

if(NOT CMAKE_CXX_FLAGS_MINSIZEREL OR (CMAKE_CXX_FLAGS_MINSIZEREL STREQUAL " ") OR (CMAKE_CXX_FLAGS_MINSIZEREL STREQUAL CMAKE_CXX_FLAGS_MINSIZEREL_INIT))
    set (CMAKE_CXX_FLAGS_MINSIZEREL "/DNDEBUG" CACHE STRING "" FORCE)
endif()

if(NOT CMAKE_C_FLAGS_RELWITHDEBINFO OR (CMAKE_C_FLAGS_RELWITHDEBINFO STREQUAL " ") OR (CMAKE_C_FLAGS_RELWITHDEBINFO STREQUAL CMAKE_C_FLAGS_RELWITHDEBINFO_INIT))
    set (CMAKE_C_FLAGS_RELWITHDEBINFO   "/DNDEBUG /Zi" CACHE STRING "" FORCE)
endif()

if(NOT CMAKE_CXX_FLAGS_RELWITHDEBINFO OR (CMAKE_CXX_FLAGS_RELWITHDEBINFO STREQUAL " ") OR (CMAKE_CXX_FLAGS_RELWITHDEBINFO STREQUAL CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT))
    set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi" CACHE STRING "" FORCE)
endif()

ADD_DEFINITIONS(-D_REENTRANT)
